package com.jinwei.qunguanjia.common.utils;

import com.alibaba.fastjson2.JSON;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * json转换工具类
 * @author yuanzs
 * 2020/12/10 15:50
 */
public class JsonUtils {
    public static String toJsonString(Object object) {
        return JSON.toJSONString(object);
    }

    public static <T> T parseObject(String json, Class<T> clazz) {
        return JSON.parseObject(json, clazz);
    }

    public static <S, T> T parseObject(S s, Class<T> clazz) {
        return JSON.parseObject(toJsonString(s), clazz);
    }

    private static <T> List<T> parseList(String json, Class<T> clazz) {
        return JSON.parseArray(json, clazz);
    }

    public static <S, T> List<T> copyList(List<S> list, Class<T> clazz){
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        return parseList(toJsonString(list), clazz);
    }
}
